package cn.stu.system.controller;


import cn.stu.common.utils.ApiData;
import cn.stu.common.utils.R;
import cn.stu.common.utils.UserHolder;
import cn.stu.framework.dto.UserDisableDto;
import cn.stu.framework.dto.UsersDetailDto;
import cn.stu.framework.service.UsersDetailService;
import cn.stu.framework.vo.UsersDetailVo;
import cn.stu.framework.vo.UsersVo;
import cn.stu.system.annotation.LogAnnotation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;


/**
 * 用户详细控制器
 *
 * @author HGZS
 * @date 2023/08/03
 * @menu 用户信息
 */
@RestController
@RequestMapping("/usersDetail")
@AllArgsConstructor
public class UsersDetailController {

    private UsersDetailService usersDetailService;

    /**
     * 查询用户
     *
     * @param dto dto
     * @return {@link ApiData}
     */
    @GetMapping("/selectContacts")
    public ApiData selectContacts(UsersDetailDto dto) {
        return usersDetailService.selectContacts(dto);
    }


    /**
     * 文件上传
     *
     * @param file 文件
     * @return {@link ApiData}
     */
    @PostMapping(value = "/uploadAvatar")
    public ApiData fileUpload(@RequestParam(required = false) MultipartFile file) {
        return usersDetailService.fileUpload(file);
    }

    /**
     * 添加
     *
     * @param urs 用户需求说明书
     * @return {@link ApiData}
     */
    @LogAnnotation("添加新用户")
    @PostMapping(value = "/add")
    public ApiData add(@RequestBody @Valid UsersDetailVo urs) {
        return usersDetailService.userAdd(urs);
    }


    /**
     * 更新
     *
     * @param urs 用户需求说明书
     * @return {@link ApiData}
     */
    @LogAnnotation("修改用户信息")
    @PutMapping(value = "/update")
    public ApiData update(@RequestBody @Valid UsersDetailVo urs) {

        return usersDetailService.userUpdate(urs);
    }

    /**
     * 删除
     *
     * @param userId 用户id
     * @param pwd    密码
     * @return {@link ApiData}
     */
    @LogAnnotation("删除用户")
    @DeleteMapping(value = "/del")
    public ApiData remove(@RequestParam("userId") Long userId, @RequestParam("pwd") String pwd) {
        return usersDetailService.userDel(userId, pwd);
    }

    /**
     * 重置密码
     *
     * @param userId 用户id
     * @return {@link ApiData}
     */
    @LogAnnotation("重置密码")
    @PostMapping(value = "/res")
    public ApiData res(@RequestParam(value = "userId", required = false) Long userId) {
        return usersDetailService.userReset(userId);
    }

    /**
     * 信息
     *
     * @return {@link ApiData}
     */
    @GetMapping(value = "/info")
    public R<UsersVo> info() {
        Long userId = UserHolder.getUser().getUserId();
        return usersDetailService.userInfo(userId);
    }

    /**
     * 禁用
     *
     * @param userDisableDto 用户禁用dto
     * @return {@link ApiData}
     */
    @LogAnnotation("启用/禁用用户")
    @PostMapping(value = "/disabled")
    public ApiData disabled(@RequestBody UserDisableDto userDisableDto) {
        return usersDetailService.userDisabled(userDisableDto);
    }

    @GetMapping(value = "/count")
    public R<Long> count() {
        return R.ok(usersDetailService.count(), "查询成功");
    }
}


